package com.flutter_ecg.flutter_ecg.ecgapplication.utils;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.view.animation.LinearInterpolator;
import android.widget.TextView;

/**
 * Created by Administrator on 2017\12\20 0020.
 */

public class ShowAnimatorUtil {

    private static AnimatorSet animatorSet_last = null;

    public static void showTextAnimationSet(final TextView view, final String text) {
        if (null == view) return;
        view.setTranslationX(0);
        AnimatorSet animatorSet = new AnimatorSet();
        final ObjectAnimator animator_out = ObjectAnimator.ofFloat(view, "translationX", 0, -view.getResources().getDisplayMetrics().widthPixels * 3 / 4);
        animator_out.setInterpolator(new LinearInterpolator());
        animator_out.setDuration(1000);
        final ObjectAnimator animator_in = ObjectAnimator.ofFloat(view, "translationX", view.getResources().getDisplayMetrics().widthPixels * 3 / 4, 0);
        animator_in.setDuration(1000);
        animator_in.setInterpolator(new LinearInterpolator());
//        animator_in.addListener();
        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                view.setText(text);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
//                taskManager.doNext();
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                animatorSet_last = null;
            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animatorSet.playSequentially(animator_out, animator_in);
        animatorSet.start();
        animatorSet_last = animatorSet;
    }

    public static void clearAnimator() {
        if (null != animatorSet_last && animatorSet_last.isRunning()) {
            try {
                animatorSet_last.cancel();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
